package dialogs
{
	import Classes.pi_events;
	import Classes.pi_images;
	
	import UIFree.ObjectEvent;
	import UIFree.components.uif_window;
	import UIFree.controls.uif_button;
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	
	import cardsbody.pi_pngcard;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class pi_effectDialog extends uif_window {
		private var _effectButton: Array;
		private var _curEffect: int;
		
		public function pi_effectDialog(parent:uif_component, ax:int=0, ay:int=0, styleClass:String='', modal:Boolean=false) {
			super(parent, ax, ay, 220, 10, styleClass, modal);
			var inner: Rectangle = this.getInner();
			with (new uif_label(this, inner.x, 0, inner.width, 100, 'description')) {
				text = pi_data.EFFECTSSPEC;
				alignApply(['left', 'top']);
			}
			_curEffect = -1;
			_effectButton = new Array();
			var i: int;
			for each (var effect: Object in pi_data.effects) {
				var by: int = i * 22 + 118;
				if (effect.paid) {
					var price: * = addChild(new pi_images.c_price());
					price.y = by;
				}
				var button: uif_button = addButton(effect, function(e: MouseEvent): void {
					selectEffect((e.target as uif_button)._data);
				});
				i++;
			}

			app.addEventListener(pi_events.FULLVIEWCHANGE, function(e: Event): void {
				if (pj.isFullView) doClose(null);
			})
			app.addEventListener(pi_events.COMPLETEFULLLOAD, function(e: ObjectEvent): void {
				if (e.object is pi_pngcard) selectEffect((e.object as pi_pngcard).effectID);
				else doClose(null);
			});
		}
		
		public function addButton(effect: Object, releaseFunc: Function): uif_button {
			var by: int = _effectButton.length * 22 + 118;
			var _label: uif_label = new uif_label(this, 45, by, 160, 20, 'collection');
			_label.text = effect.name;
			var button: uif_button = new uif_button(this, 30, by + 4, 10, 10, 'collectButton');
			button.addEventListener(MouseEvent.CLICK, releaseFunc);
			button._data = effect.id;
			_effectButton.push(button);
			resize(width, _effectButton.length * 22 + 140);
			return button;
		} 
		
		private function selectEffect(effectID: int): void {
			if (!effectID) effectID = 0;
			if (_curEffect != effectID) {
				if (_curEffect > -1) _effectButton[pi_data.effectIndex(_curEffect)].styleClass = 'collectButton';
				_curEffect = effectID;
				if (_curEffect > -1) _effectButton[pi_data.effectIndex(_curEffect)].styleClass = 'collectButtonCheck';
				app.dispatchEvent(new ObjectEvent(pi_events.CHANGEEFFECT, _curEffect));
			}
		}
		
		protected function get pj(): photo_inject {
			return app as photo_inject; 
		}
		
		override public function show(): void {
			var effectID: int = (pj.editor.cardBody as pi_pngcard)?(pj.editor.cardBody as pi_pngcard).effectID:0;
			selectEffect(effectID);
			super.show();
		}
		
		override protected function canClose(): int {
			return 1;
		}
		
	}
}